/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.service.guarantor;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.base.dao.guarantor.GuarantorDao;
import com.jeesite.modules.base.entity.guarantor.Guarantor;
import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 担保方Service
 * @author lv
 * @version 2021-02-24
 */
@Service
@Transactional(readOnly=true)
public class GuarantorService extends CrudService<GuarantorDao, Guarantor> {

    @Autowired
    private CodeRuleService codeRuleService;
    /**
     * 获取单条数据
     * @param guarantor
     * @return
     */
    @Override
    public Guarantor get(Guarantor guarantor) {
        return super.get(guarantor);
    }

    /**
     * 查询分页数据
     * @param guarantor 查询条件
     * @param guarantor.page 分页对象
     * @return
     */
    @Override
    public Page<Guarantor> findPage(Guarantor guarantor) {
        return super.findPage(guarantor);
    }

    /**
     * 保存数据（插入或更新）
     * @param guarantor
     */
    @Override
    @Transactional(readOnly=false)
    public void save(Guarantor guarantor) {
        if (guarantor.getIsNewRecord()){
            guarantor.setGuarantorId(codeRuleService.getCode("guarantee"));
            guarantor.setCreateBy(UserUtils.getUser().getUserCode());
            guarantor.setUpdateBy(UserUtils.getUser().getUserCode());
        }else {
            guarantor.setUpdateBy(UserUtils.getUser().getUserCode());
        }
        super.save(guarantor);
        if (guarantor.getIsNewRecord()){
            codeRuleService.updateCurrentNumber("guarantee");
        }
        // 保存上传附件
        FileUploadUtils.saveFileUpload(guarantor.getId(), "guarantor_file");
    }

    /**
     * 更新状态
     * @param guarantor
     */
    @Override
    @Transactional(readOnly=false)
    public void updateStatus(Guarantor guarantor) {
        super.updateStatus(guarantor);
    }

    /**
     * 删除数据
     * @param guarantor
     */
    @Override
    @Transactional(readOnly=false)
    public void delete(Guarantor guarantor) {
        super.delete(guarantor);
    }

}